package com.htl.exampro.question.controller;

import cn.hutool.core.bean.BeanUtil;
import com.htl.exampro.base.model.PageResult;
import com.htl.exampro.base.model.ServerResponse;
import com.htl.exampro.question.pojo.domain.QuestionDO;
import com.htl.exampro.question.pojo.query.QuestionPageParam;
import com.htl.exampro.question.pojo.vo.QuestionVO;
import com.htl.exampro.question.service.QuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 试题表 前端控制器
 * </p>
 *
 * @author hehehe
 * @since 2025-05-25
 */
@Tag(name = "题目管理", description = "题目相关接口")
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Operation(summary = "查询题目", description = "根据主键查询题目信息")
    @GetMapping("/{id}")
    public ServerResponse<QuestionVO> queryQuestionById(@PathVariable("id") String id) {
        QuestionVO questionVO = questionService.queryQuestionById(id);
        return ServerResponse.success(questionVO);
    }

    @Operation(summary = "查询题目", description = "根据主键批量查询题目信息")
    @PostMapping("/list")
    public ServerResponse<List<QuestionVO>> queryQuestionByIds(@RequestBody List<Long> ids) {
        if (ids.isEmpty())
            return ServerResponse.fail("主键不能为空！");
        List<QuestionDO> questionDOS = questionService.listByIds(ids);
        List<QuestionVO> questionVOS = BeanUtil.copyToList(questionDOS, QuestionVO.class);
        return ServerResponse.success(questionVOS);
    }

    @Operation(summary = "分页查询题目", description = "支持按条件分页查询题目列表")
    @PostMapping("/page")
    public ServerResponse<PageResult<QuestionVO>> queryQuestionPage(@Validated @RequestBody QuestionPageParam pageParam) {
        PageResult<QuestionVO> pageResult = questionService.queryQuestionPage(pageParam);
        return ServerResponse.success(pageResult);
    }

    @Operation(summary = "保存题目", description = "保存题目")
    @PostMapping("")
    public ServerResponse<Integer> saveQuestion(@Validated @RequestBody QuestionVO questionVO) {
        int i = questionService.saveQuestion(questionVO);
        return ServerResponse.success(i);
    }

    @Operation(summary = "修改题目", description = "修改题目")
    @PutMapping("")
    public ServerResponse<Integer> updateQuestion(@Validated @RequestBody QuestionVO questionVO) {
        int i = questionService.updateQuestion(questionVO);
        return ServerResponse.success(i);
    }

    @Operation(summary = "删除题目", description = "根据主键删除题目信息")
    @DeleteMapping("/{id}")
    public ServerResponse<Boolean> delQuestionById(@PathVariable("id") String id) {
        return ServerResponse.success(questionService.removeById(id));
    }

}
